package com.bieyang.anjia.ui.adapter;

import java.util.List;

import org.webrtc.ui.VideoUI;

import com.bieyang.anjia.R;
import com.bieyang.anjia.module.Deviceinfo;
import com.bieyang.anjia.ui.activity.AlarmSetActivity;
import com.bieyang.anjia.ui.activity.App;
import com.bieyang.anjia.utils.Show;
import com.bieyang.anjia.utils.SubUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DeviceAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private PopupWindow pw;
	View view;
	public TextView txtPlay, txtResetName, txtSetZhuji, txtRemove, txtjilu;
	int currentPosition;
	public ProgressDialog progressDialog;
	// private TAlarmSetInfor alarmInfo;
	public String imsi;
	Context con;
	List<Deviceinfo> deviceList;
	Deviceinfo devInfo;
	String uuid;
	String devName;

	public DeviceAdapter(Context con) {
		this.con = con;
		inflater = LayoutInflater.from(con);
	}

	public DeviceAdapter(Context con, List<Deviceinfo> deviceList) {
		// this.deviceList = deviceList;
		this.deviceList = deviceList;
		this.con = con;
		inflater = LayoutInflater.from(con);

	}

	public List<Deviceinfo> getDeviceList() {
		return deviceList;
	}

	public void setDeviceList(List<Deviceinfo> deviceList) {
		this.deviceList = deviceList;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return deviceList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
//		Log.d(""+position, deviceList.get(position).online+"-islogin:"+App.isLogin);
		
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.device_manage_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.position = position;
			if (0 != deviceList.size()) {
				devInfo = deviceList.get(position);
//				Log.d("online", devInfo.online + "-" + devInfo.uuid);
				if ("ONLINE".equals(devInfo.online)) {
					holder.imgLive
							.setBackgroundResource(R.drawable.live_online);

				} else {
					holder.imgLive
							.setBackgroundResource(R.drawable.live_offline);
				}
				if (!devInfo.uuid.equals(SubUtils.getUuid(con)))
					holder.tvCaption.setText(devInfo.name);
			}

		return convertView;
	}

	public class ViewHolder {
		ImageView imgLive;
		TextView tvCaption;
		ImageView imgArrow;
		RelativeLayout rlInfo;
		int position;

		public ViewHolder(View convertView) {
			imgLive = (ImageView) convertView.findViewById(R.id.imgLive);
			tvCaption = (TextView) convertView.findViewById(R.id.tvCaption);
			imgArrow = (ImageView) convertView.findViewById(R.id.imgArrow);
			rlInfo = (RelativeLayout) convertView.findViewById(R.id.rlInfo);
			imgArrow.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (null == deviceList || deviceList.size() == 0) {
						Show.toast(con, R.string.network_error);
						return;
					}
					Intent intent = new Intent(con, AlarmSetActivity.class);
					intent.putExtra("id", deviceList.get(position).id);
					intent.putExtra("uuid", deviceList.get(position).uuid);
					intent.putExtra("name", deviceList.get(position).name);
					con.startActivity(intent);
				}
			});
			rlInfo.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (App.isLogin) {
						if (0 != deviceList.size()) {
							if (!"ONLINE".equals(deviceList.get(position).online)) {
								Show.toast(con, R.string.tip_live_offline);
								return;
							}
							devName = deviceList.get(position).name;
							uuid = deviceList.get(position).uuid;
							startCall(uuid, devName);
						}
					}

				}
			});
		}
	}

	private void startCall(String to, String devName) {
		VideoUI.Lauch(con, to, devName, true, true, false);
	}

}
